layui.use(['form','element','jquery','layer','laydate'], function(){
	var $=layui.jquery,form=layui.form,laydate = layui.laydate;
	//时间转化
	function showDate(longTypeDate){
		if(longTypeDate==undefined){
			return "未设置";
		}
		var dateType = ""; 
		var date = new Date(); 
		date.setTime(longTypeDate); 
		dateType += date.getFullYear();  //年 
		dateType += "-" + getMonth(date); //月  
		dateType += "-" + getDay(date);  //日 
		return dateType;
	} 
	//返回 01-12 的月份值  
	function getMonth(date){ 
		var month = ""; 
		month = date.getMonth() + 1; //getMonth()得到的月份是0-11 
		if(month<10){ 
			month = "0" + month; 
		} 
		return month; 
	} 
	//返回01-30的日期 
	function getDay(date){ 
		var day = ""; 
		day = date.getDate(); 
		if(day<10){ 
			day = "0" + day; 
		} 
		return day; 
	}
	$(function(){
		$("#addtj").click(function(){
			var num=$("span[name=num]").length;
			var txt=$("<div name='txt'><header class='larry-personal-tit'><i class='layui-icon'></i>条件<span name='num'>"+(num+1)+"</span></header></div>");
			var t=$("<textarea placeholder='请输入要求' class='layui-textarea newCondition'></textarea>");
			txt.append(t);
			$(this).before(txt);
			t.trigger("focus");
		});
		$("#deletetj").click(function(){
			var txt=$("div[name=txt]").last();
			txt.remove();
			txt.trigger("focus");
		});
		
		$("#add").click(function(){
			var num=$(".newsEndTime").length;
			if(num>=1)
			{
				return;
			}
			var t=$("<div class='layui-input-inline'><input type='text' class='layui-input datetime newsEndTime' lay-verify='date'></div>");
			$(this).before(t);
			form.render(); 
	        timeAdd();
			$("#delete").click(function(){
				t.remove();
			});
			t.trigger("focus");
			createdate();
		});
		
		function createdate(){
			laydate.render({
				elem: 'input[name=date]:eq(1)' //指定元素
				});
		}
		//核心方法 
		function timeAdd(){ 
			lay('.datetime').each(function() { 
			laydate.render({ elem : this, trigger : 'click'}); 
			});
		}
		
		//下拉框
		$("div[name=riicount] select").html("");
		for(var i=1;i<=100;i++)
		{
			$("div[name=riicount] select").append("<option value="+i+">"+i+"</option>");
		}
		
		
		
		var riid=$(".riid").val(); //得到被选中的值
		//修改
		form.on("submit(update)",function(data){
			var index = layer.msg('修改中，请稍候...',{icon: 16,time:false,shade:0.8});
			//获取值
			var riaddress=$(".newsAddress").val();
			var riicount=$("div[name=riicount] dl .layui-this").attr("lay-value");
			var riiendTime=$(".newsEndTime").val();
			//任职条件
			var list = $("div[name='txt'] textarea");
			var rirequirement="";
			for(var i=0;i<list.length;i++)
			{
				rirequirement+= $("div[name='txt'] textarea").eq(i).val();
				if(i<list.length-1)
				{
					rirequirement+=";";
				}
			}
			$.ajax({
				type:"post",
				url:"../recruitmentManagementAction!updateRecruitinfo",
				async:false,
				dataType:'json',
				data:{riid:riid,riaddress:riaddress,riicount:riicount,riiendTime:riiendTime,rirequirement:rirequirement},
				success:function show(z){
					layer.close(index);
					if(z)
					{
						layer.msg('修改成功', {icon: 1});
					}else{
						layer.msg('修改失败', {icon: 5});
					}
				},error:function(){
	 				location.href="404.jsp";
	 			}
			});
		});
		
		
		$(".default").click(function(data){
			show();
		});
		
		show();
		function show(){
			//查询相应对象
			$.ajax({
				type:"post",
				url:"../recruitmentManagementAction!riidShowRecruitinfo",
				async:false,
				dataType:'json',
				data:{riid:riid},
				success:function show(z){
					//清空
					$("#gwzz").html("");
					$("input[name=ptIname]").val(z.positioninfo.ptIname);
					$(".newsTime").val(showDate(z.riibeginTime));
					$(".newsAddress").val(z.riaddress);
					$(".newsNum").find("option[value='"+z.riicount+"']").prop("selected",true);
					form.render();
					var str0=(z.positioninfo.ptIrequire).split(";");
					for(var i=0;i<str0.length;i++)
					{
						$("#gwzz").append("<i class='layui-icon '></i>要求<span>"+(i+1)+".</span>"+str0[i]+"；");
						if(i<str0.length-1){
							$("#gwzz").append("<br/>");
						}
					}
					$("div[name=txt]").remove();
					var str1=(z.rirequirement).split(";");
					for(var i=0;i<str1.length;i++){
						var num=$("span[name=num]").length;
						var txt=$("<div name='txt'><header class='larry-personal-tit'><i class='layui-icon'></i>条件<span name='num'>"+(num+1)+"</span></header></div>");
						var t=$("<textarea placeholder='请输入要求' class='layui-textarea newCondition'>"+str1[i]+"</textarea>");
						txt.append(t);
						$("#addtj").before(txt);
						t.trigger("focus");
					}
					$(".time").remove();
					if(z.riiendTime!=0)
					{
						var t=$("<div class='layui-input-inline time'><input type='text' value='"+showDate(z.riiendTime)+"' class='layui-input datetime newsEndTime' lay-verify='date'></div>");
						$("#add").before(t);
						form.render(); 
				        timeAdd();
						$("#delete").click(function(){
							t.remove();
						});
						t.trigger("focus");
						createdate();
					}
				},error:function(){
		 			location.href="404.jsp";
		 		}
			});
			form.render('select');
			timeAdd();
		}
		
	});
});